﻿Date.prototype.pattern = function (fmt) {
  var o = {
    'M+': this.getMonth() + 1, //月份
    'd+': this.getDate(), //日
    'h+': this.getHours() % 12 == 0 ? 12 : this.getHours() % 12, //小时
    'H+': this.getHours(), //小时
    'm+': this.getMinutes(), //分
    's+': this.getSeconds(), //秒
    'q+': Math.floor((this.getMonth() + 3) / 3), //季度
    'S': this.getMilliseconds() //毫秒
  };
  var week = {
    '0': '/u65e5',
    '1': '/u4e00',
    '2': '/u4e8c',
    '3': '/u4e09',
    '4': '/u56db',
    '5': '/u4e94',
    '6': '/u516d'
  };
  // 正则替换年
  if (/(y+)/.test(fmt)) {
    fmt = fmt.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length));
  }
  // 正则替换周
  if (/(E+)/.test(fmt)) {
    fmt = fmt.replace(RegExp.$1, (RegExp.$1.length > 1 ? (RegExp.$1.length > 2 ? '/u661f/u671f' : '/u5468') : '') + week[this.getDay() + '']);
  }
  // 遍历正则替换
  for (var k in o) {
    if (new RegExp('(' + k + ')').test(fmt)) {
      fmt = fmt.replace(RegExp.$1, RegExp.$1.length === 1 ? o[k] : ('00' + o[k]).substr(('' + o[k]).length));
    }
  }
  return fmt;
};

var allPageSize = 10;

var loading = {
  show: function () {
    $('#loading').modal('show');
  },
  hide: function () {
    $('#loading').modal('hide');
  }
};

// 分页初始化
function pagesSet(obj) {
  var pcPageInfo = {};
  pcPageInfo = {
    currentPage: obj.current,
    totalCount: obj.totalRow,
    pageSize: obj.pageSize,
    firstText: '<i class="glyphicon glyphicon-fast-backward"></i>',
    prevText: '<i class="glyphicon glyphicon-step-backward"></i>',
    nextText: '<i class="glyphicon glyphicon-step-forward"></i>',
    lastText: '<i class="glyphicon glyphicon-fast-forward"></i>',
    selecter: $('#pages'),
    firstFn: function (i) {
      loading.show();
      obj.current = parseInt(i, 10);
      console.log('firstFn', obj.current);
      showPage(obj);
      // fn1();
    },
    lastFn: function (i) {
      loading.show();
      obj.current = parseInt(i, 10);
      console.log('lastFn', obj.current);
      // fn1();
      showPage(obj);
    },
    prevFn: function (i) {
      loading.show();
      obj.current = parseInt(i, 10);
      console.log('prevFn', obj.current);
      // fn1();
      showPage(obj);
    },
    nextFn: function (i) {
      loading.show();
      obj.current = parseInt(i, 10);
      console.log('nextFn', obj.current);
      // fn1();
      showPage(obj);
    },
    noFn: function (i) {
      loading.show();
      obj.current = parseInt(i, 10);
      // console.log('no', obj.current);
      // fn1();
      showPage(obj);
    }
  };
  // 总页数
  // totalPage = pcPageInfo.totalCount % pcPageInfo.pageSize == 0 ? pcPageInfo.totalCount / pcPageInfo.pageSize : parseInt(pcPageInfo.totalCount / pcPageInfo.pageSize) + 1;
  totalPage = obj.totalPage;
  pcPages();

  // pc分页
  function pcPages() {
    var commentPage = new pages(pcPageInfo);
    commentPage.init();
  }
}

function setPageSize(o) {
  $('#pageSize').on('change', function () {
    var v = $(this).find(':selected').val();
    o.pageSize = parseInt(v, 10);
    var totalPage = o.totalRow % o.pageSize == 0 ? o.totalRow / o.pageSize : parseInt(o.totalRow / o.pageSize) + 1;
    if (o.current > totalPage) {
      o.current = totalPage
    }
    console.log(o)
    // fn1();
    showPage(o);
  });
}

function navEvent(callback) {
  $('#nav').on('click', 'li', function () {
    $(this).addClass('active').siblings().removeClass('active');
    var index = $(this).index();
    callback(index);
  });
}

function getUrlId() {
  var id = window.location.href;
  id = id.slice(id.lastIndexOf('/') + 1);
  return id;
}

// 去除文字两边空白
function trim(string) {
  return string.replace(/^\s+|\s+$/g, "");
}

// 获取字典
function GetDictionarys(key, callback) {
  $.ajax({
    url: '/api/CommonApi?API=GetDictionarys',
    type: 'get',
    data: {Key: key, Text: '', Logogram: ''},
    dataType: 'json',
    success: function (data) {
      if (data.Success) {
        callback(data);
      }
    }
  });
}

// 生成26个字母
function getAToZ() {
  var A_Z = [];
  for (var j = 65; j < 91; j++) {
    A_Z.push(String.fromCharCode(j));
  }
  return A_Z;
}

//
function checkTeacher(o) {
  var i = 0;
  var obj = o.data2;
  var l = obj.length;
  o.teacher = false;
  o.run = false;
  o.pm = false;
  o.finance = false;
  o.reject = false;
  for (; i < l; i++) {
    if (obj[i].IncidentContent.indexOf('驳回') !== -1) {
      o.reject = true;
    }
    if (obj[i].TriggerMan === '项目人员' && obj[i].IncidentContent === '提交申请') {
      o.teacher = true;
    } else if (obj[i].TriggerMan === '项目运营' && obj[i].IncidentContent === '审核通过') {
      o.run = true;
    } else if (obj[i].TriggerMan === '项目经理' && obj[i].IncidentContent === '审核通过') {
      o.pm = true;
    } else if (obj[i].TriggerMan === '财务' && obj[i].IncidentContent === '审核通过') {
      o.finance = true;
    }
  }
}

// 搜索
var searchObj = {
  ProjectName: '',
  StartDate: '',
  EndDate: ''
};
searchBox();
function searchBox() {
  var btn = $('#search-box label[for="search"]');
  btn.click(function () {
    loading.show();
    var search = $('#search-box #search').val();
    var timeStart = $('#search-box .datepk')[0].value;
    var timeEnd = $('#search-box .datepk')[1].value;
    search = search || '';
    timeStart = timeStart || '';
    timeEnd = timeEnd || '';
    searchObj = {
      ProjectName: search,
      StartDate: timeStart,
      EndDate: timeEnd
    };
    // console.log(searchObj)
    showPage(lsInfo);
  });
}


// selectAllProjects(function (data) {
//   console.log(data)
//   var i = 0;
//   var obj = data.ApiParamObj;
//   var l = obj.length;
//   var options = '<option value="" selected="selected">请选择</option>';
//   for (; i < l; i++) {
//     options += '<option value="' + obj[i].ProjectID + '">' + obj[i].ProjectName + '</option>';
//   }
//   $('#allProject').html(options).select2();
// });

function getAllBrandsAndProjects(callback) {
  // 获取所有项目
  $.ajax({
    url: '/api/CommonApi?API=ITGetProjects',
    type: 'get',
    dataType: 'json',
    success: function (data) {
      if (data.Success) {
        callback(data);
      }
    }
  });
}

function selectAllProjects(callback) {
  // 获取所有项目
  $.ajax({
    url: '/api/CommonApi?API=ITGetMyProjects',
    type: 'get',
    data: {ProjectName: '', ProjectNameSpell: ''},
    dataType: 'json',
    success: function (data) {
      if (data.Success) {
        callback(data);
      }
    }
  });
}

// 判断是否是数组
function isArray(arg) {
  return Object.prototype.toString.call(arg) === '[object Array]';
}

// 判断是不是日期
function checkDate(date) {
  return (new Date(date).getDate() == date.substring(date.length - 2));
}

// 去重
function unique(arr) {
  var res = [];
  var json = {};
  for (var i = 0; i < arr.length; i++) {
    if (!json[arr[i]]) {
      json[arr[i]] = true;


      res.push(arr[i]);
    }
  }
  return res;
}

var moduleLevel = 3;

var confirmC = function (callback) {
  $('#confirm .confirm').off('click').on('click', function () {
    callback(true);
  });
  $('#confirm .cancel').off('click').on('click', function () {
    callback(false);
  });
};